package frontend.pages.admin.project;

import backend.model.admin.Employee;
import backend.model.admin.ForeignManager;
import backend.service.admin.EmployeeService;
import backend.service.admin.ForeignManagerService;
import backend.service.admin.ProjectService;
import frontend.core.TimeTrackingWebApplication;
import frontend.model.admin.ProjectUIModel;
import frontend.pages.admin.base.AbstractManagingForm;
import frontend.components.base.ComponentFactory;
import frontend.components.other.DBModelChoiceRenderer;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.ArrayList;
import java.util.List;

public class ProjectForm extends AbstractManagingForm<ProjectUIModel> {

    private static final String RESOURCE_KEY_FIELD_LABEL_NAME = "pages.admin.project.form.name";
    private static final String RESOURCE_KEY_FIELD_LABEL_DESCRIPTION = "pages.admin.project.form.description";
    private static final String RESOURCE_KEY_FIELD_LABEL_IS_DIRECT_PAYMENT = "pages.admin.project.form.isDirectPayment";
    private static final String RESOURCE_KEY_FIELD_LABEL_IS_ACTIVE = "pages.admin.project.form.isActive";
    private static final String RESOURCE_KEY_FIELD_LABEL_PM_RO = "pages.admin.project.form.pmRomania";
    private static final String RESOURCE_KEY_FIELD_LABEL_PM_NO = "pages.admin.project.form.pmNorway";

    private static final String RESOURCE_KEY_VALIDATION_SAME_PROJECT_NAME = "validation.form.project.sameProjectName";

	public ProjectForm(String id, ProjectUIModel projectUIModel) {
		super(id, projectUIModel);		
	}

	@Override
	protected void constructFormContent() {
        Panel nameComponent = ComponentFactory.createTextFieldComponent("name", RESOURCE_KEY_FIELD_LABEL_NAME, uiModel);
        Panel descriptionComponent = ComponentFactory.createTextFieldComponent("description", RESOURCE_KEY_FIELD_LABEL_DESCRIPTION, uiModel);
        Panel isDirectPaymentComponent = ComponentFactory.createCheckboxComponent("isDirectPayment", RESOURCE_KEY_FIELD_LABEL_IS_DIRECT_PAYMENT, uiModel);
        Panel isActiveComponent = ComponentFactory.createCheckboxComponent("isActive", RESOURCE_KEY_FIELD_LABEL_IS_ACTIVE, uiModel);

        Panel pmRomaniaComponent = ComponentFactory.createDropDownChoiceComponent("pmRomania", RESOURCE_KEY_FIELD_LABEL_PM_RO, uiModel,
                getAvailableEmployees(), new DBModelChoiceRenderer<Employee>(), true);
        Panel pmNorwayComponent = ComponentFactory.createDropDownChoiceComponent("pmNorway", RESOURCE_KEY_FIELD_LABEL_PM_NO, uiModel,
                getAvailableForeignManagers(), new DBModelChoiceRenderer<ForeignManager>(), true);

		add(nameComponent, descriptionComponent, isActiveComponent, isDirectPaymentComponent, pmRomaniaComponent, pmNorwayComponent);
	}

    @Override
    protected Class getResponsePage() {
        return ProjectManagingPage.class;
    }

    @Override
    protected void doFormValidation() {
        validateNotEmpty(uiModel.getName(), RESOURCE_KEY_FIELD_LABEL_NAME);
        validateNotEmpty(uiModel.getDescription(), RESOURCE_KEY_FIELD_LABEL_DESCRIPTION);
        if ((uiModel.getName() != null) && nameWasChanged()) {
            ProjectService service =  TimeTrackingWebApplication.getService(ProjectService.class);
            if (service.findProjectByName(uiModel.getName()) != null) {
                addFieldValidationError(RESOURCE_KEY_FIELD_LABEL_NAME, RESOURCE_KEY_VALIDATION_SAME_PROJECT_NAME);
            }
        }
    }

    private boolean nameWasChanged() {
        return (false == uiModel.getName().equals(uiModel.getDBModel().getName()));
    }

    private List<Employee> getAvailableEmployees() {
        List<Employee> availableEmployees = new ArrayList<Employee>();
        availableEmployees.addAll(TimeTrackingWebApplication.getService(EmployeeService.class).findAllActiveEmployees());
        return availableEmployees;
    }

    private List<ForeignManager> getAvailableForeignManagers() {
        List<ForeignManager> availableManagers = new ArrayList<ForeignManager>();
        availableManagers.addAll(TimeTrackingWebApplication.getService(ForeignManagerService.class).findAllActiveForeignManagers());
        return availableManagers;
    }
}
